﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessTier.Facade.Implementation;
using BusinessTier.Facade.Interface;
using Domain;

namespace WebMVC.Areas.AdminPanel.Controllers
{
    public class CustomerController : AdminControllerBase
    {
        private IOrderService orderService;
        

        public CustomerController() : this(new OrderService())
        {
            
        }

        public CustomerController(IOrderService orderService)
        {
            this.orderService = orderService;
        }

        public ActionResult Index()
        {
            return View(orderService.GetAllCustomers());
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {
                orderService.SaveCustomer(customer);
                base.Info = String.Format("New customer \"{0}\" save successfully", customer.DisplayName);
                return RedirectToAction("Index");
            }
            return View(customer);
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {

            return View(orderService.GetCustomerById(id));
        }

        [HttpPost]
        public ActionResult Edit(Customer customer)
        {
            if (ModelState.IsValid)
            {
                orderService.SaveCustomer(customer);
                base.Info = String.Format("Changes to customer \"{0}\" saved successfully", customer.DisplayName);
                return RedirectToAction("Index");
            }
            return View(customer);
        }

    }
}
